package cn.iocoder.yudao.module.contract.controller.admin.basicData;

import cn.iocoder.yudao.module.contract.controller.admin.order.vo.OrderSyncReqVO;
import cn.iocoder.yudao.module.contract.dal.dataobject.basicData.BasicPaymentDO;
import cn.iocoder.yudao.module.contract.webservice.ContractWebService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.contract.controller.admin.basicData.vo.*;
import cn.iocoder.yudao.module.contract.dal.dataobject.basicData.BasicSupplierDO;
import cn.iocoder.yudao.module.contract.service.basicData.BasicSupplierService;

@Tag(name = "管理后台 - 供应商")
@RestController
@RequestMapping("/contract/basic-supplier")
@Validated
public class BasicSupplierController {

    @Resource
    private BasicSupplierService basicSupplierService;
    @Resource
    private ContractWebService contractWebService;

    @PostMapping("/create")
    @Operation(summary = "创建供应商")
    @PreAuthorize("@ss.hasPermission('contract:basic-supplier:create')")
    public CommonResult<Long> createBasicSupplier(@Valid @RequestBody BasicSupplierSaveReqVO createReqVO) {
        return success(basicSupplierService.createBasicSupplier(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新供应商")
    @PreAuthorize("@ss.hasPermission('contract:basic-supplier:update')")
    public CommonResult<Boolean> updateBasicSupplier(@Valid @RequestBody BasicSupplierSaveReqVO updateReqVO) {
        basicSupplierService.updateBasicSupplier(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除供应商")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('contract:basic-supplier:delete')")
    public CommonResult<Boolean> deleteBasicSupplier(@RequestParam("id") Long id) {
        basicSupplierService.deleteBasicSupplier(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得供应商")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('contract:basic-supplier:query')")
    public CommonResult<BasicSupplierRespVO> getBasicSupplier(@RequestParam("id") Long id) {
        BasicSupplierDO basicSupplier = basicSupplierService.getBasicSupplier(id);
        return success(BeanUtils.toBean(basicSupplier, BasicSupplierRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得供应商分页")
    @PreAuthorize("@ss.hasPermission('contract:basic-supplier:query')")
    public CommonResult<PageResult<BasicSupplierRespVO>> getBasicSupplierPage(@Valid BasicSupplierPageReqVO pageReqVO) {
        PageResult<BasicSupplierDO> pageResult = basicSupplierService.getBasicSupplierPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BasicSupplierRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出供应商 Excel")
    @PreAuthorize("@ss.hasPermission('contract:basic-supplier:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBasicSupplierExcel(@Valid BasicSupplierPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BasicSupplierDO> list = basicSupplierService.getBasicSupplierPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "供应商.xls", "数据", BasicSupplierRespVO.class,
                        BeanUtils.toBean(list, BasicSupplierRespVO.class));
    }

    @PostMapping("/supplierSync")
    @Operation(summary = "供应商同步")
    public CommonResult<?> supplierSync(@RequestBody OrderSyncReqVO syncReqVO) {
        String result = contractWebService.getSupplier(syncReqVO.getEndDate(), syncReqVO.getVbeln());
        if(result.contains("同步失败-")){
            return error(result);
        }
        return success(result);
    }
    @GetMapping("/list")
    @Operation(summary = "获得供应商列表")
    public CommonResult<List<BasicSupplierDO>> getBasicSupplierList(@Valid BasicSupplierDO basicSupplierDO) {
        List<BasicSupplierDO> listResult = basicSupplierService.list(Wrappers.lambdaQuery(basicSupplierDO));
        return success(listResult);
    }
}